package com.xindong.paking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xindong.paking.dtos.PageResult;
import com.xindong.paking.entity.TbBalance;
import com.xindong.paking.entity.TbUser;
import com.xindong.paking.entity.constant.Constants;
import com.xindong.paking.mapper.TbUserMapper;
import com.xindong.paking.service.TbBalanceService;
import com.xindong.paking.service.TbUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindong.paking.utils.BCrypt;
import com.xindong.paking.utils.Result;
import com.xindong.paking.utils.ResultStatus;
import com.xindong.paking.utils.UserInfoUtil;
import io.netty.util.internal.StringUtil;
import org.apache.ibatis.session.ResultContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wenbo
 * @since 2023-01-07
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService {

    @Autowired
    private TbUserMapper mapper;

    @Autowired
    private TbBalanceService tbBalanceService;

    @Override
    @Transactional
    public Result addUser(TbUser user) {

        // 判断用户是否存在
        QueryWrapper<TbUser> tbUserQueryWrapper = new QueryWrapper<>();
        tbUserQueryWrapper.eq("username", user.getUsername());
        TbUser tbUser = mapper.selectOne(tbUserQueryWrapper);
        if (tbUser != null) {
            return new Result(ResultStatus.USER_HAS_EXISTED);
        }

        // 判断手机号是否存在
        tbUserQueryWrapper = new QueryWrapper<>();
        tbUserQueryWrapper.eq("phone", user.getPhone());
        tbUser = mapper.selectOne(tbUserQueryWrapper);
        if (tbUser != null) {
            return new Result(ResultStatus.USER_PHONE_EXISTED);
        }

        // 加密密码
        // 随机盐
        String gensalt = BCrypt.gensalt();
        String hashpw = BCrypt.hashpw(user.getPassword(), gensalt);
        user.setPassword(hashpw);

        // 创建时间
        user.setCreateTime(LocalDateTime.now());
        // 更新时间
        user.setUpdateTime(LocalDateTime.now());

        // 用户状态
        user.setStatus(1);

        // 用户权限
        user.setRole(1);

        mapper.insert(user);

        // 插入余额表
        TbBalance tbBalance = new TbBalance();
        tbBalance.setUserId(user.getId());
        tbBalanceService.add(tbBalance);

        return new Result(ResultStatus.SUCCESS);
    }

    @Override
    public Result deleteUser(Integer id) {

        mapper.deleteById(id);

        return Result.success("删除成功", null);
    }

    @Override
    public Result updateUser(TbUser user) {

        // 当前操作用户信息
        Map<String, Object> userInfo = UserInfoUtil.getUserInfo();

        // 操作更新的用户id
        user.setUpdateUser(userInfo.get("id").toString());

        // 更新时间
        user.setUpdateTime(LocalDateTime.now());

        // 判断是否为修改密码
        String password = user.getPassword();
        if (password != null) {

            // 密码加密
            String gensalt = BCrypt.gensalt();
            String hashpw = BCrypt.hashpw(password, gensalt);
            user.setPassword(hashpw);

        }

        mapper.updateById(user);

        return Result.success("更新成功", null);
    }

    @Override
    public Result list(Integer page, Integer pageSize) {

        // 设置分页
        IPage<TbUser> ipage = new Page<>(page, pageSize);

        // 进行分页查询
        IPage<TbUser> tbUserIPage = mapper.selectPage(ipage, null);

        // 获取分页后数据，并把密码设为空
        List<TbUser> records = tbUserIPage.getRecords();
        for (TbUser record : records) {
            record.setPassword(null);
        }

        // 分页返回数据
        PageResult result = new PageResult(records, tbUserIPage.getTotal(), page, pageSize);

        return Result.success("获取数据成功", result);
    }

    @Override
    public Result selectById(Integer id) {
        TbUser tbUser = mapper.selectById(id);
        return Result.success(Constants.DEFAULT_UPDATE_SUCCESS_MSG,tbUser);
    }
}
